package com.sitech.openapi.service;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sitech.openapi.componet.BusiConst;
import com.sitech.openapi.domain.customer.*;
import com.sitech.openapi.domain.user.*;
import com.sitech.openapi.mapper.*;
import com.sitech.openapi.utils.DateUtil;
import com.sitech.openapi.utils.NumberUtil;
import com.sitech.openapi.utils.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.*;

@Service
@Slf4j
public class UserCmptServiceImpl implements UserCmptService {

    @Autowired
    private UserCmptMapper userCmptMapper;
    @Autowired
    private PaymentPlanMapper paymentPlanMapper;
    @Autowired
    private CustMapper custMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private PartyMapper partyMapper;
    @Autowired
    private PartyCertificationMapper partyCertificationMapper;
    @Autowired
    private PartyContactInfoMapper partyContactInfoMapper;
    @Autowired
    private CreditLimitMapper creditLimitMapper;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private AccountMapper accountMapper;
    @Autowired
    private ProdInstMapper prodInstMapper;
    @Autowired
    private ProdOfferMapper prodOfferMapper;
    @Autowired
    private ProductMapper productMapper;
    @Autowired
    private ProdInstAccNbrMapper prodInstAccNbrMapper;
    @Autowired
    private ProdInstAttrMapper prodInstAttrMapper;
    @Autowired
    private ProdInstAcctMapper prodInstAcctMapper;
    @Autowired
    private ProdResInstRelMapper prodResInstRelMapper;
    @Autowired
    private ProdOfferInstMapper prodOfferInstMapper;
    @Autowired
    private ProdInstNbrUimMapper prodInstNbrUimMapper;
    @Autowired
    private ProdOfferEffexpMapper prodOfferEffexpMapper;
    @Autowired
    private ProdOfferInstAttrMapper prodOfferInstAttrMapper;
    @Autowired
    private OfferProdInstRelMapper offerProdInstRelMapper;
    @Autowired
    private OfferResInstRelMapper offerResInstRelMapper;


    @Override
    public Map<String, Object> getAccountInfoByAccNum(Map<String, Object> paraMap) {

        Map<String, Object> resMap = new HashMap<>();
        List<Account> accounts = new ArrayList<>();

        String accNum = StringUtil.obj2Str(paraMap.get("accNum"));
        Integer pageSize = StringUtils.isEmpty(paraMap.get("pageSize")) ? 30 : (Integer) paraMap.get("pageSize");
        Integer pageIndex = StringUtils.isEmpty(paraMap.get("pageIndex")) ? 1 : (Integer) paraMap.get("pageIndex");

        PageHelper.startPage(pageIndex, pageSize);

        List<CAccount> CAccounts = userCmptMapper.getAccountByNumber(accNum);
        if( CAccounts == null || CAccounts.size() == 0) {
            throw new RuntimeException("根据号码未查询到账户信息，请核实号码是否存在！");
        }
        Page<CAccount> page = (Page<CAccount>) CAccounts;

        accounts = getAccountList(CAccounts);

        resMap.put("dataList", accounts);
        resMap.put("total", page.getTotal());

        return resMap;
    }

    /**
     * 根据号码或者 客户id 查询用户信息
     * 包含以下信息：
     * 客户	customers
     * 账户 accounts
     * 产品实例 prodInsts
     * 销售品实例 offerInsts
     * 卡实例 CardInsts
     * 终端实例 TermInsts
     *
     * @param paraMap 入参信息
     * @return 实体列表
     */
    @Override
    public Map<String, Object> getUserInfo(Map<String, Object> paraMap) {

        String custId = StringUtil.obj2Str(paraMap.get("custId"));
        String regionId = StringUtil.obj2Str(paraMap.get("regionId"));
        String accNum = StringUtil.obj2Str(paraMap.get("accNum"));
        String searchType = StringUtil.obj2Str(paraMap.get("searchType"));
        Integer pageSize = StringUtils.isEmpty(paraMap.get("pageSize")) ? 30 : (Integer) paraMap.get("pageSize");
        Integer pageIndex = StringUtils.isEmpty(paraMap.get("pageIndex")) ? 1 : (Integer) paraMap.get("pageIndex");
        PageHelper.startPage(pageIndex, pageSize);
        Long total = 0L;

        String lanId = "";
        if(StringUtil.isNotEmptyOrNull(regionId) && regionId.length() > 6){
            lanId = regionId.substring(1,5);
        }
        // 定义反参
        Map<String, Object> resMap = new HashMap<>();
        List<User> users = new ArrayList<>();
        User user = new User();
        List<Customer> customers = new ArrayList<>();
        List<Account> accounts = new ArrayList<>();

        if (StringUtil.isNotEmptyOrNull(custId)) {
            // 1、查客户信息
            List<Cust> custs = new ArrayList<>();
            Cust cust = custMapper.selectByPrimaryKey(Long.parseLong(custId));
            if (cust == null) {
                throw new RuntimeException("根据客户id查询客户信息为空！");
            }
            /*if(StringUtil.isNotEmptyOrNull(lanId)){
                if(!StringUtil.isEqual(cust.getLanId(),lanId)){
                    throw new RuntimeException("客户归属与regionId不匹配！");
                }
            }*/
            custs.add(cust);
            customers = customerService.getCustomerByCust(custs);
            //Page<Cust> page = (Page<Cust>) custs;// todo 应该是有问题
            //total = page.getTotal();


            // 2、查产品实例
            List<CProdInst> CProdInsts = new ArrayList<>();
            ProdInstExample prodInstExample = new ProdInstExample();
            prodInstExample.createCriteria().andOwnerCustIdEqualTo(Long.parseLong(custId)).andProductIdEqualTo(BusiConst.PRODUCT_WLW);
            CProdInsts = prodInstMapper.selectByExample(prodInstExample);


            // 3、根据产品实例查账户
            List<CAccount> CAccounts = new ArrayList<>();
            List<ProdInstAcct> prodInstAccts = new ArrayList<>();
            if (CProdInsts != null && CProdInsts.size() > 0) {
                for (CProdInst CProdInst : CProdInsts) {
                    ProdInstAcctExample prodInstAcctExample = new ProdInstAcctExample();
                    prodInstAcctExample.createCriteria().andProdInstIdEqualTo(CProdInst.getAccProdInstId());
                    prodInstAccts.addAll(prodInstAcctMapper.selectByExample(prodInstAcctExample));
                }
                for (ProdInstAcct prodInstAcct : prodInstAccts) {
                    CAccount CAccount = accountMapper.selectByPrimaryKey(prodInstAcct.getAccountId());
                    if (!CAccounts.contains(CAccount)) {
                        CAccounts.add(CAccount);
                    }
                }
            }
            accounts = getAccountList(CAccounts);

            users = getUserInfoByProdInst(CProdInsts, custs, customers, accounts);
            total = 1L;
        } else {
            // 根据号码查询
            if (StringUtil.isNotEmptyOrNull(accNum) && StringUtil.isNotEmptyOrNull(searchType)) {
                if ("0".equals(searchType)) {
                    // 查询当前接入号下同账户的虚拟合账号码
                    ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
                    prodInstAccNbrExample.createCriteria().andAccNbrEqualTo(accNum);

                    List<ProdInstAccNbr> prodInstAccNbrs = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
                    if (prodInstAccNbrs != null && prodInstAccNbrs.size() > 0) {
                        ProdInstAcctExample prodInstAcctExample = new ProdInstAcctExample();
                        prodInstAcctExample.createCriteria().andProdInstIdEqualTo(prodInstAccNbrs.get(0).getProdInstId());
                        List<ProdInstAcct> prodInstAccts = prodInstAcctMapper.selectByExample(prodInstAcctExample);
                        if (prodInstAccts != null && prodInstAccts.size() > 0) {
                            // 查同账户下的所有用户实例信息
                            prodInstAcctExample.clear();
                            prodInstAcctExample.createCriteria().andAccountIdEqualTo(prodInstAccts.get(0).getAccountId());
                            List<ProdInstAcct> prodInstAccts1 = prodInstAcctMapper.selectByExample(prodInstAcctExample);
                            // 查用户实例信息
                            List<CProdInst> CProdInsts = new ArrayList<>();
                            if (prodInstAccts1 != null && prodInstAccts1.size() > 0) {
                                for (ProdInstAcct prodInstAcct : prodInstAccts1) {
                                    ProdInstExample prodInstExample = new ProdInstExample();
                                    prodInstExample.createCriteria().andAccProdInstIdEqualTo(prodInstAcct.getProdInstId()).andProductIdEqualTo(BusiConst.PRODUCT_WLW);
                                    CProdInsts.addAll(prodInstMapper.selectByExample(prodInstExample));
                                }

                            }
                            List<Cust> custs = new ArrayList<>();
                            if (CProdInsts != null && CProdInsts.size() > 0) {
                                Cust cust = custMapper.selectByPrimaryKey(CProdInsts.get(0).getOwnerCustId());
                                if(cust == null){
                                    throw new RuntimeException("根据实例未查询到客户信息！");
                                }
                                /*if(StringUtil.isNotEmptyOrNull(lanId)){
                                    if(!StringUtil.isEqual(cust.getLanId(),lanId)){
                                        throw new RuntimeException("客户归属与regionId不匹配！");
                                    }
                                }*/
                                custs.add(cust);
                            }else{
                                throw new RuntimeException("根据该号码查询用户实例为空！或者该号码不是物联网合账号码!");
                            }
                            if (custs != null && custs.size() > 0) {
                                customers = customerService.getCustomerByCust(custs);
                            }

                            // 查账户信息
                            List<CAccount> CAccounts = new ArrayList<>();
                            CAccount CAccount = accountMapper.selectByPrimaryKey(prodInstAccts.get(0).getAccountId());
                            CAccounts.add(CAccount);
                            accounts = getAccountList(CAccounts);

                            users = getUserInfoByProdInst(CProdInsts, custs, customers, accounts);
                            total = 1L;
                        }


                    } else {
                        throw new RuntimeException("根据号码查询用户信息为空！");
                    }


                } else if ("1".equals(searchType)) {
                    // 查询当前接入号下虚拟合账号码
                    ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
                    prodInstAccNbrExample.createCriteria().andAccNbrEqualTo(accNum);

                    List<ProdInstAccNbr> prodInstAccNbrs = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
                    if (prodInstAccNbrs != null && prodInstAccNbrs.size() > 0) {
                        ProdInstAcctExample prodInstAcctExample = new ProdInstAcctExample();
                        prodInstAcctExample.createCriteria().andProdInstIdEqualTo(prodInstAccNbrs.get(0).getProdInstId());
                        List<ProdInstAcct> prodInstAccts = prodInstAcctMapper.selectByExample(prodInstAcctExample);
                        if (prodInstAccts != null && prodInstAccts.size() > 0) {
                            // 查用户实例信息
                            List<CProdInst> CProdInsts = new ArrayList<>();
                            CProdInst cProdInst = new CProdInst();
                            cProdInst = prodInstMapper.selectByPrimaryKey(prodInstAccNbrs.get(0).getProdInstId());
                            if(cProdInst == null || !Objects.equals(cProdInst.getProductId(), BusiConst.PRODUCT_WLW)){
                                throw new RuntimeException("根据该号码查询用户实例为空！或者该号码不是物联网合账号码!");
                            }
                            CProdInsts.add(cProdInst);

                            List<Cust> custs = new ArrayList<>();
                            if (CProdInsts.size() > 0) {
                                Cust cust = custMapper.selectByPrimaryKey(CProdInsts.get(0).getOwnerCustId());
                                if(cust == null){
                                    throw new RuntimeException("根据实例未查询到客户信息！");
                                }
                                /*if(StringUtil.isNotEmptyOrNull(lanId)){
                                    if(!StringUtil.isEqual(cust.getLanId(),lanId)){
                                        throw new RuntimeException("客户归属与regionId不匹配！");
                                    }
                                }*/
                                custs.add(cust);
                            }
                            if (custs.size() > 0) {
                                customers = customerService.getCustomerByCust(custs);
                            }

                            // 查账户信息
                            List<CAccount> CAccounts = new ArrayList<>();
                            CAccount CAccount = accountMapper.selectByPrimaryKey(prodInstAccts.get(0).getAccountId());
                            CAccounts.add(CAccount);
                            accounts = getAccountList(CAccounts);

                            users = getUserInfoByProdInst(CProdInsts, custs, customers, accounts);
                            total = 1L;
                        }
                    } else {
                        throw new RuntimeException("根据号码查询用户信息为空！");
                    }
                }
            }

        }
        resMap.put("total", total);
        resMap.put("dataList", users);
        return resMap;
    }

    private List<User> getUserInfoByProdInst(List<CProdInst> CProdInsts, List<Cust> custs, List<Customer> customers, List<Account> accounts) {

        List<User> users = new ArrayList<>();
        if (custs != null && custs.size() > 0) {
            for (Cust cust : custs) {
                User user = new User();
                List<ProdInst> prodInsts = new ArrayList<>();
                List<OfferInst> offerInsts = new ArrayList<>();
                List<CardInst> cardInsts = new ArrayList<>();
                List<TermInst> termInsts = new ArrayList<>();

                if (CProdInsts != null && CProdInsts.size() > 0) {

                    for (CProdInst CProdInst : CProdInsts) {
                        ProdInst prodInst = new ProdInst();
                        prodInst.setProdInstId(CProdInst.getProdInstId());
                        prodInst.setExtProdInstId(CProdInst.getExtProdInstId());
                        Product product = productMapper.selectByPrimaryKey(CProdInst.getProductId());
                        prodInst.setProdNbr(product.getProductNbr());
                        prodInst.setProdName(product.getProductName());
                        prodInst.setAccProdInstId(CProdInst.getAccProdInstId());
                        prodInst.setExtAccProdInstId(CProdInst.getExtProdInstId());
                        //prodInst.setProdUseType();
                        List<ProdInstAccNbr> prodInstAccNbrs = new ArrayList<>();
                        ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
                        prodInstAccNbrExample.createCriteria().andProdInstIdEqualTo(CProdInst.getAccProdInstId());
                        prodInstAccNbrs = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
                        prodInst.setAccNum(prodInstAccNbrs.get(0).getAccNbr());
                        prodInst.setPaymentModeCd(CProdInst.getPaymentModeCd());
                        prodInst.setOwnerCustId(CProdInst.getOwnerCustId());
                        if (StringUtil.isNotEmptyOrNull(cust.getExtCustId())) {
                            prodInst.setExtOwnerCustId(Long.parseLong(cust.getExtCustId()));
                        }
                        //prodInst.setProdInstPwd();
                        prodInst.setRegionId(CProdInst.getCommonRegionId());
                        prodInst.setLanId(CProdInst.getLanId());
                        prodInst.setActDate(DateUtil.toStringYmdHmsWthH(CProdInst.getCreateDate()));
                        prodInst.setBeginRentDate(DateUtil.toStringYmdHmsWthH(CProdInst.getBeginRentTime()));
                        prodInst.setStopRentDate(DateUtil.toStringYmdHmsWthH(CProdInst.getStopRentTime()));
                        prodInst.setProdInstStatus(CProdInst.getStatusCd());
                        prodInst.setFirstFinishDate(DateUtil.toStringYmdHmsWthH(CProdInst.getFinishTime()));
                        prodInst.setUseCustId(CProdInst.getUseCustId());
                        Cust userCust = custMapper.selectByPrimaryKey(CProdInst.getUseCustId());
                        prodInst.setExtUseCustId(userCust.getExtCustId());
                        //prodInst.setRespCustId();
                        //prodInst.setExtRespCustId();
                        //prodInst.setRemark();
                        // 产品实例属性
                        List<ProdInstAttr> prodInstAttrs = new ArrayList<>();
                        List<CProdInstAttr> CProdInstAttrs = new ArrayList<>();
                        ProdInstAttrExample prodInstAttrExample = new ProdInstAttrExample();
                        prodInstAttrExample.createCriteria().andProdInstIdEqualTo(CProdInst.getAccProdInstId());
                        CProdInstAttrs = prodInstAttrMapper.selectByExample(prodInstAttrExample);
                        if (CProdInstAttrs != null && CProdInstAttrs.size() > 0) {
                            for (CProdInstAttr CProdInstAttr : CProdInstAttrs) {
                                ProdInstAttr prodInstAttr = new ProdInstAttr();
                                prodInstAttr.setAttrId(CProdInstAttr.getAttrId());
                                prodInstAttr.setAttrValue(CProdInstAttr.getAttrValue());
                                prodInstAttrs.add(prodInstAttr);
                            }
                        }
                        prodInst.setProdInstAttrs(prodInstAttrs);
                        // 产品账务关系
                        List<ProdInstAcct> prodInstAccts = new ArrayList<>();
                        List<ProdInstAcctRel> prodInstAcctRels = new ArrayList<>();
                        ProdInstAcctExample prodInstAcctExample = new ProdInstAcctExample();
                        prodInstAcctExample.createCriteria().andProdInstIdEqualTo(CProdInst.getAccProdInstId());
                        prodInstAccts = prodInstAcctMapper.selectByExample(prodInstAcctExample);
                        if (prodInstAccts != null && prodInstAccts.size() > 0) {
                            for (ProdInstAcct prodInstAcct : prodInstAccts) {
                                ProdInstAcctRel prodInstAcctRel = new ProdInstAcctRel();

                                prodInstAcctRel.setProdInstAcctRelId(prodInstAcct.getProdInstAcctId());
                                prodInstAcctRel.setExtProdInstAcctRelId(prodInstAcct.getExtProdInstAcctId());
                                prodInstAcctRel.setAcctId(prodInstAcct.getAccountId());
                                CAccount CAccount = accountMapper.selectByPrimaryKey(prodInstAcct.getAccountId());
                                prodInstAcctRel.setExtAcctId(CAccount.getExtAccountId());
                                prodInstAcctRel.setPriority(prodInstAcct.getPriority());
                                //prodInstAcctRel.setPaymentLimitType(Long.parseLong(prodInstAcct.getPaymentLimitType()));
                                prodInstAcctRel.setPaymentLimit(prodInstAcct.getPaymentLimit());
                                //prodInstAcctRel.setUpperAmount();
                                prodInstAcctRel.setEffDate(DateUtil.toStringYmdHmsWthH(prodInstAcct.getEffDate()));
                                prodInstAcctRel.setExpDate(DateUtil.toStringYmdHmsWthH(prodInstAcct.getExpDate()));
                                prodInstAcctRel.setIfDefaultAccId(Long.parseLong(CAccount.getIfDefault()));
                                prodInstAcctRel.setProdInstId(CProdInst.getAccProdInstId());
                                prodInstAcctRel.setExtProdInstId(CProdInst.getExtProdInstId());
                                prodInstAcctRel.setStatusCd(prodInstAcct.getStatusCd());
                                prodInstAcctRel.setStatusDate(DateUtil.toStringYmdHmsWthH(prodInstAcct.getStatusDate()));
                                //prodInstAcctRel.setRemark();
                                prodInstAcctRels.add(prodInstAcctRel);
                            }
                        }
                        prodInst.setProdInstAcctRels(prodInstAcctRels);
                        // 查询产品实例营销资源实例关系
                        List<ProdResInstRel> prodResInstRels = new ArrayList<>();
                        List<GprodResInstRel> gprodResInstRels = new ArrayList<>();
                        ProdResInstRelExample prodResInstRelExample = new ProdResInstRelExample();
                        prodResInstRelExample.createCriteria().andProdInstIdEqualTo(CProdInst.getAccProdInstId());
                        prodResInstRels = prodResInstRelMapper.selectByExample(prodResInstRelExample);
                        if (prodResInstRels != null && prodResInstRels.size() > 0) {
                            for (ProdResInstRel prodResInstRel : prodResInstRels) {
                                GprodResInstRel gprodResInstRel = new GprodResInstRel();
                                gprodResInstRel.setProdResInstRelId(prodResInstRel.getProdResInstRelId());
                                gprodResInstRel.setProdInstId(prodResInstRel.getProdInstId());
                                gprodResInstRel.setExtProdInstId(CProdInst.getExtProdInstId());
                                gprodResInstRel.setMktResInstNbr(StringUtil.obj2Str(prodResInstRel.getMktResInstId()));
                                //gprodResInstRel.setMktResId();
                                //gprodResInstRel.setUseProdInstId();
                                gprodResInstRel.setRelType(prodResInstRel.getTypeCd());
                                gprodResInstRel.setMktResNum(prodResInstRel.getMktResNum());
                                gprodResInstRel.setStatusCd(prodResInstRel.getStatusCd());
                                //gprodResInstRel.setLastOrderItemId();
                                //gprodResInstRel.setRemark();

                                gprodResInstRels.add(gprodResInstRel);

                            }
                        }
                        prodInst.setGprodResInstRels(gprodResInstRels);
                        //prodInst.setStopType();
                        prodInsts.add(prodInst);

                        // 4、查销售品实例
                        List<ProdOfferInst> prodOfferInsts = new ArrayList<>();
                        ProdOfferInstExample prodOfferInstExample = new ProdOfferInstExample();
                        prodOfferInstExample.createCriteria().andProdInstIdEqualTo(CProdInst.getAccProdInstId());
                        prodOfferInsts = prodOfferInstMapper.selectByExample(prodOfferInstExample);

                        if (prodOfferInsts != null && prodOfferInsts.size() > 0) {
                            for (ProdOfferInst prodOfferInst : prodOfferInsts) {
                                OfferInst offerInst = new OfferInst();
                                offerInst.setOfferInstId(prodOfferInst.getProdOfferInstId());
                                offerInst.setExtOfferInstId(prodOfferInst.getExtProdOfferInstId());
                                ProdOffer prodOffer = new ProdOffer();
                                prodOffer = prodOfferMapper.selectByPrimaryKey(prodOfferInst.getProdOfferId());
                                offerInst.setOfferNbr(prodOffer.getOfferNbr());
                                offerInst.setOfferType(Long.parseLong(prodOffer.getOfferType()));
                                offerInst.setOffName(prodOffer.getProdOfferName());
                                offerInst.setOwnerCustId(cust.getCustId());
                                if(StringUtil.isNotEmptyOrNull(cust.getExtCustId())){
                                    offerInst.setExtOwnerCustId(Long.parseLong(cust.getExtCustId()));
                                }
                                offerInst.setEffDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getEffDate()));
                                offerInst.setExpDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getExpDate()));
                                //offerInst.setOfferAgreeId();
                                offerInst.setCreateOrgId(prodOffer.getCreateRegion());
                                offerInst.setExpProcMethod(prodOffer.getExpProcMethod());
                                //offerInst.setEffType();// 生效方式
                                offerInst.setLanId(prodOfferInst.getLanId());
                                offerInst.setRegionId(prodOfferInst.getCommonRegionId());
                                offerInst.setStatusCd(prodOfferInst.getStatusCd());
                                offerInst.setBusiModDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getStatusDate()));
                                offerInst.setCreateDate(DateUtil.toStringYmdHmsWthH(prodOfferInst.getCreateDate()));
                                //offerInst.setRemark();
                                // 销售品属性实例
                                List<ProdOfferInstAttr> prodOfferInstAttrs = new ArrayList<>();
                                List<OfferInstAttr> offerInstAttrs = new ArrayList<>();
                                ProdOfferInstAttrExample prodOfferInstAttrExample = new ProdOfferInstAttrExample();
                                prodOfferInstAttrExample.createCriteria().andProdOfferInstIdEqualTo(prodOfferInst.getProdInstId());
                                prodOfferInstAttrs = prodOfferInstAttrMapper.selectByExample(prodOfferInstAttrExample);
                                if (prodOfferInstAttrs != null && prodOfferInstAttrs.size() > 0) {
                                    for (ProdOfferInstAttr prodOfferInstAttr : prodOfferInstAttrs) {
                                        OfferInstAttr offerInstAttr = new OfferInstAttr();
                                        offerInstAttr.setAttrId(prodOfferInstAttr.getAttrId());
                                        offerInstAttr.setAttrValue(prodOfferInstAttr.getAttrValue());
                                        offerInstAttrs.add(offerInstAttr);

                                    }
                                }
                                offerInst.setOfferInstAttrs(offerInstAttrs);
                                // 查询产品销售品实例关系
                                List<COfferProdInstRel> COfferProdInstRels = new ArrayList<>();
                                List<OfferProdInstRel> offerProdInstRels = new ArrayList<>();
                                OfferProdInstRelExample offerProdInstRelExample = new OfferProdInstRelExample();
                                offerProdInstRelExample.createCriteria().andProdInstIdEqualTo(prodOfferInst.getProdInstId());
                                COfferProdInstRels = offerProdInstRelMapper.selectByExample(offerProdInstRelExample);
                                if (COfferProdInstRels != null && COfferProdInstRels.size() > 0) {
                                    for (COfferProdInstRel COfferProdInstRel : COfferProdInstRels) {
                                        OfferProdInstRel offerProdInstRel = new OfferProdInstRel();
                                        offerProdInstRel.setOfferProdInstRelId(COfferProdInstRel.getOfferProdInstRelId());
                                        //offerProdInstRel.setExtOfferProdInstRelId();
                                        offerProdInstRel.setOfferInstId(prodOfferInst.getProdOfferInstId());
                                        offerProdInstRel.setExtOfferInstId(prodOfferInst.getExtProdOfferInstId());
                                        offerProdInstRel.setProdInstId(COfferProdInstRel.getProdInstId());
                                        offerProdInstRel.setExtProdInstId(CProdInst.getExtProdInstId());
                                        offerProdInstRel.setRoleCd(StringUtil.obj2Str(COfferProdInstRel.getRoleCd()));
                                        //offerProdInstRel.setRoleName();
                                        //offerProdInstRel.setStatusCd();
                                        //offerProdInstRel.setRemark();
                                        offerProdInstRel.setAccNum(prodInstAccNbrs.get(0).getAccNbr());
                                        offerProdInstRel.setEffDate(DateUtil.toStringYmdHmsWthH(COfferProdInstRel.getEffDate()));
                                        offerProdInstRel.setExpDate(DateUtil.toStringYmdHmsWthH(COfferProdInstRel.getExpDate()));

                                        offerProdInstRels.add(offerProdInstRel);

                                    }
                                }
                                offerInst.setOfferProdInstRels(offerProdInstRels);
                                // 销售品实例营销资源实例关系
                                List<COfferResInstRel> COfferResInstRels = new ArrayList<>();
                                List<OfferResInstRel> offerResInstRels = new ArrayList<>();
                                OfferResInstRelExample offerResInstRelExample = new OfferResInstRelExample();
                                offerResInstRelExample.createCriteria().andProdOfferInstIdEqualTo(prodOfferInst.getProdOfferInstId());
                                COfferResInstRels = offerResInstRelMapper.selectByExample(offerResInstRelExample);
                                if (COfferResInstRels != null && COfferResInstRels.size() > 0) {
                                    for (COfferResInstRel COfferResInstRel : COfferResInstRels) {
                                        OfferResInstRel offerResInstRel = new OfferResInstRel();
                                        offerResInstRel.setOfferResInstRelId(COfferResInstRel.getOfferResInstRelId());
                                        //offerResInstRel.setExtOfferResInstRelId();
                                        offerResInstRel.setOfferInstId(prodOfferInst.getProdOfferInstId());
                                        offerResInstRel.setExtOfferInstId(prodOfferInst.getExtProdOfferInstId());
                                        offerResInstRel.setMktResInstNbr(StringUtil.obj2Str(COfferResInstRel.getMktResInstId()));
                                        //offerResInstRel.setRemark();
                                        offerResInstRels.add(offerResInstRel);
                                    }
                                }
                                offerInst.setOfferResInstRels(offerResInstRels);
                                offerInsts.add(offerInst);
                            }
                        }
                        // 5、查卡实例
                        List<MktResInst1198> mktResInst1198s = new ArrayList<>();
                        mktResInst1198s = userCmptMapper.getMktResInstByProdInstId(StringUtil.obj2Str(CProdInst.getAccProdInstId()));
                        if (mktResInst1198s != null && mktResInst1198s.size() > 0) {
                            for (MktResInst1198 mktResInst1198 : mktResInst1198s) {
                                CardInst cardInst = new CardInst();
                                cardInst.setMktResInstId(mktResInst1198.getMktResInstId());
                                //cardInst.setMktResInstNbr();
                                cardInst.setMktResId(mktResInst1198.getMktResId());
                                //cardInst.setMktResNbr();
                                cardInst.setMktResStoreId(mktResInst1198.getMktResStoreId());
                                //cardInst.setMktResTypeNbr();
                                cardInst.setLanId(CProdInst.getLanId());
                                cardInst.setStatusDate(DateUtil.toStringYmdHmsWthH(mktResInst1198.getStatusDate()));
                                cardInst.setStatusCd(mktResInst1198.getStatusCd());
                                //cardInst.setRemark();
                                //cardInst.setCardInstAttrs();
                                cardInsts.add(cardInst);
                            }
                        }

                    }
                }
                // 6、查终端实例

                user.setCustomers(customers);
                user.setAccounts(accounts);
                user.setProdInsts(prodInsts);
                user.setOfferInsts(offerInsts);
                user.setCardInsts(cardInsts);
                user.setTermInsts(termInsts);
                users.add(user);
            }
        }

        return users;
    }

    private List<Account> getAccountList(List<CAccount> CAccounts) {

        List<Account> accounts = new ArrayList<>();
        if (CAccounts != null && CAccounts.size() > 0) {
            Cust cust = custMapper.selectByPrimaryKey(CAccounts.get(0).getCustId());
            String extcustId = cust.getExtCustId();
            for (CAccount CAccount : CAccounts) {
                Account account = new Account();
                account.setAcctId(CAccount.getAccountId());
                account.setAcctName(CAccount.getAccountName());
                //account.setAcctCd();
                //account.setAcctBillingType();
                //account.setEffDate();
                //account.setExpDate();
                account.setExtAcctId(CAccount.getExtAccountId());
                account.setCustId(CAccount.getCustId());
                if (StringUtil.isNotEmptyOrNull(extcustId)) {
                    account.setExtCustId(extcustId);
                }
                account.setRegionId(CAccount.getCommonRegionId());
                account.setStatusCd(CAccount.getStatusCd());
                account.setStatusDate(DateUtil.toStringYmdHmsWthH(CAccount.getStatusDate()));
                //account.setRemark();
                List<PaymentPlan> paymentPlans = new ArrayList<>();
                List<CPaymentPlan> CPaymentPlans = new ArrayList<>();
                PaymentPlanExample paymentPlanExample = new PaymentPlanExample();
                paymentPlanExample.createCriteria().andAccountIdEqualTo(CAccount.getAccountId());
                CPaymentPlans = paymentPlanMapper.selectByExample(paymentPlanExample);
                if (CPaymentPlans != null && CPaymentPlans.size() > 0) {
                    for (CPaymentPlan CPaymentPlan : CPaymentPlans) {
                        PaymentPlan paymentPlan = new PaymentPlan();
                        paymentPlan.setPaymentPlanId(CPaymentPlan.getPaymentPlanId());
                        paymentPlan.setExtPaymentPlanId(CPaymentPlan.getExtPaymentPlanId());
                        //paymentPlan.setPayAcctType();
                        //paymentPlan.setPayAcctId();
                        paymentPlan.setAcctId(CAccount.getAccountId());
                        paymentPlan.setExtAcctId(CAccount.getExtAccountId());
                        if (StringUtil.isNotEmptyOrNull(CPaymentPlan.getPaymentMethodCd())) {
                            paymentPlan.setPaymentMethod(Long.parseLong(CPaymentPlan.getPaymentMethodCd()));
                        }
                        paymentPlan.setPriority(CPaymentPlan.getPriority());
                        //paymentPlan.setPaymentLimitType();
                        //paymentPlan.setPaymentLimit();
                        //paymentPlan.setUpperAmount();
                        //paymentPlan.setIfOnlinePay();
                        paymentPlan.setStatusCd(CPaymentPlan.getStatusCd());
                        if(StringUtil.isNotEmptyOrNull(CPaymentPlan.getStatusDate())){
                            paymentPlan.setStatusDate(DateUtil.toStringYmdHmsWthH(CPaymentPlan.getStatusDate()));
                        }
                        //paymentPlan.setRemark();
                        paymentPlans.add(paymentPlan);
                    }
                }
                account.setPaymentPlans(paymentPlans);
                accounts.add(account);
            }
        }
        return accounts;
    }

    /**
     * 根据客户id/号码/lte_imsi
     * 包含以下信息：
     * 客户id	custId
     * c网号码 accNum
     * 宽带号码 account
     * lImsi lImsi
     * 产品实例类型 prodUseType
     *
     * @param paraMap 入参信息
     * @return 实体列表
     */
    @Override
    public Map<String, Object> queryProdInstByPara(Map<String, Object> paraMap){
        // 定义反参
        Map<String, Object> resMap = new HashMap<>();
        String custId = StringUtil.obj2Str(paraMap.get("custId"));
        String account = StringUtil.obj2Str(paraMap.get("account"));
        String accNum = StringUtil.obj2Str(paraMap.get("accNum"));
        String lImsi = StringUtil.obj2Str(paraMap.get("lImsi"));
        String prodUseType = StringUtil.obj2Str(paraMap.get("prodUseType"));
        List<Long> prodInstList = new ArrayList<>();
        List<ProdInst> prodInsts = new ArrayList<>();
        if(StringUtil.isNotEmptyOrNull(custId)){
            //客户id
            ProdInstExample prodInstExample = new ProdInstExample();
            prodInstExample.createCriteria().andUseCustIdEqualTo(NumberUtil.parseLong(custId));
            List<CProdInst> cProdInstList = (List<CProdInst>)prodInstMapper.selectByExample(prodInstExample);
            for(CProdInst cProdInst:cProdInstList){
                prodInstList.add(cProdInst.getProdInstId());
            }
        }else if(StringUtil.isNotEmptyOrNull(account)){
            //宽带账号
            ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
            prodInstAccNbrExample.createCriteria().andAccNbrTypeCdEqualTo(2).andAccNbrEqualTo(account);
            List<ProdInstAccNbr> prodInstAccNbrList = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
            if(prodInstAccNbrList != null && prodInstAccNbrList.size() > 0){
                prodInstList.add(prodInstAccNbrList.get(0).getProdInstId());
            }
        }else if(StringUtil.isNotEmptyOrNull(accNum)){
            //c网账号
            ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
            prodInstAccNbrExample.createCriteria().andAccNbrTypeCdEqualTo(4).andAccNbrEqualTo(accNum);
            List<ProdInstAccNbr> prodInstAccNbrList = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
            if(prodInstAccNbrList != null && prodInstAccNbrList.size() > 0){
                prodInstList.add(prodInstAccNbrList.get(0).getProdInstId());
            }
        }else if(StringUtil.isNotEmptyOrNull(lImsi)){
            //lImsi
            ProdInstNbrUimExample prodInstNbrUimExample = new ProdInstNbrUimExample();
            prodInstNbrUimExample.createCriteria().andLteImsiEqualTo(lImsi);
            List<ProdInstNbrUim> prodInstNbrUimList = (List<ProdInstNbrUim>)prodInstNbrUimMapper.selectByExample(prodInstNbrUimExample);
            if(prodInstNbrUimList != null && prodInstNbrUimList.size() > 0){
                prodInstList.add(prodInstNbrUimList.get(0).getProdInstId());
            }
        }
        if(StringUtil.isEqual(prodUseType,"1000")||StringUtil.isEmptyOrNull(prodUseType)){
            //直接查主产品实例信息
            for (Long prodInstId : prodInstList) {
                CProdInst cProdInst = prodInstMapper.selectByPrimaryKey(prodInstId);
                if(StringUtil.isNotEmptyOrNull(cProdInst)){
                    ProdInst prodInst = new ProdInst();
                    prodInst.setProdInstId(cProdInst.getProdInstId());
                    prodInst.setExtProdInstId(cProdInst.getExtProdInstId());
                    Product product = productMapper.selectByPrimaryKey(cProdInst.getProductId());
                    prodInst.setProdNbr(product.getProductNbr());
                    prodInst.setProdName(product.getProductName());
                    prodInst.setAccProdInstId(cProdInst.getAccProdInstId());
                    prodInst.setExtAccProdInstId(cProdInst.getExtProdInstId());
                    //prodInst.setProdUseType();
                    List<ProdInstAccNbr> prodInstAccNbrs = new ArrayList<>();
                    ProdInstAccNbrExample prodInstAccNbrExample = new ProdInstAccNbrExample();
                    prodInstAccNbrExample.createCriteria().andProdInstIdEqualTo(cProdInst.getAccProdInstId());
                    prodInstAccNbrs = prodInstAccNbrMapper.selectByExample(prodInstAccNbrExample);
                    prodInst.setAccNum(prodInstAccNbrs.get(0).getAccNbr());
                    prodInst.setPaymentModeCd(cProdInst.getPaymentModeCd());
                    prodInst.setOwnerCustId(cProdInst.getOwnerCustId());
                    Cust cust = custMapper.selectByPrimaryKey(cProdInst.getOwnerCustId());
                    if (StringUtil.isNotEmptyOrNull(cust.getExtCustId())) {
                        prodInst.setExtOwnerCustId(Long.parseLong(cust.getExtCustId()));
                    }
                    //prodInst.setProdInstPwd();
                    String regionId = "8"+cProdInst.getLanId()+"00";
                    prodInst.setRegionId(NumberUtil.parseLong(regionId));
                    prodInst.setLanId(NumberUtil.parseLong(regionId));
                    prodInst.setActDate(DateUtil.toStringYmdHmsWthH(cProdInst.getCreateDate()));
                    prodInst.setBeginRentDate(DateUtil.toStringYmdHmsWthH(cProdInst.getBeginRentTime()));
                    prodInst.setStopRentDate(DateUtil.toStringYmdHmsWthH(cProdInst.getStopRentTime()));
                    prodInst.setProdInstStatus(cProdInst.getStatusCd());
                    prodInst.setFirstFinishDate(DateUtil.toStringYmdHmsWthH(cProdInst.getFinishTime()));
                    prodInst.setUseCustId(cProdInst.getUseCustId());
                    Cust userCust = custMapper.selectByPrimaryKey(cProdInst.getUseCustId());
                    prodInst.setExtUseCustId(userCust.getExtCustId());
                    //prodInst.setRespCustId();
                    //prodInst.setExtRespCustId();
                    //prodInst.setRemark();
                    // 产品实例属性
                    List<ProdInstAttr> prodInstAttrs = new ArrayList<>();
                    List<CProdInstAttr> CProdInstAttrs = new ArrayList<>();
                    ProdInstAttrExample prodInstAttrExample = new ProdInstAttrExample();
                    prodInstAttrExample.createCriteria().andProdInstIdEqualTo(cProdInst.getAccProdInstId());
                    CProdInstAttrs = prodInstAttrMapper.selectByExample(prodInstAttrExample);
                    if (CProdInstAttrs != null && CProdInstAttrs.size() > 0) {
                        for (CProdInstAttr CProdInstAttr : CProdInstAttrs) {
                            ProdInstAttr prodInstAttr = new ProdInstAttr();
                            prodInstAttr.setAttrId(CProdInstAttr.getAttrId());
                            prodInstAttr.setAttrValue(CProdInstAttr.getAttrValue());
                            prodInstAttrs.add(prodInstAttr);
                        }
                    }
                    prodInst.setProdInstAttrs(prodInstAttrs);

                    //prodInst.setStopType();
                    prodInsts.add(prodInst);
                }
            }
        }
        if(StringUtil.isEqual(prodUseType,"2000")||StringUtil.isEmptyOrNull(prodUseType)){
            for (Long prodInstId : prodInstList) {
                List<ProdInst> prodInsts1 = (List<ProdInst>)userCmptMapper.queryProdInstByAccProdInst(prodInstId);
                prodInsts.addAll(prodInsts1);
            }
        }
        resMap.put("prodInsts",prodInsts);
        return resMap;
    }
}
